package bb.lanxing.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import bb.lanxing.R;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;
import in.srain.cube.views.ptr.util.PtrLocalDisplay;

public class BikeHeader extends View implements PtrUIHandler {
    private static final int MODE_FULL_ROTATE = 7;
    private static final int MODE_FULL_STATIC = 6;
    private static final int MODE_PART_1 = 1;
    private static final int MODE_PART_2 = 2;
    private static final int MODE_PART_3 = 3;
    private static final int MODE_PART_4 = 4;
    private static final int MODE_PART_5 = 5;
    private float mAnimOffsetBegin;
    private float mAnimOffsetEnd;
    private Bitmap[] mBikeParts;
    private int mDrawZoneHeight;
    private int mDrawZoneWidth;
    private boolean mIsInLoading;
    private int mOffsetX;
    private int mOffsetY;
    private int mPaddingTop;
    private final Paint mPaint;
    private float mPart4OffsetX;
    private float mPart4OffsetY;
    private float mPart5OffsetX;
    private float mPart5OffsetY;
    private float mProgress;
    private float mRotateDegree;

    private float getRealProgress(float f, float f2, float f3) {
        if (f2 < f3 && f >= f2) {
            if (f <= f3) {
                return (f - f2) / (f3 - f2);
            }
            return 1.0f;
        }
        return 0.0f;
    }

    @Override
    public void onUIRefreshPrepare(PtrFrameLayout ptrFrameLayout) {
    }

    @Override
    public void onUIReset(PtrFrameLayout ptrFrameLayout) {
    }

    public BikeHeader(Context context) {
        this(context, null);
    }

    public BikeHeader(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public BikeHeader(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.mPaint = new Paint();
        this.mProgress = 0.0f;
        this.mIsInLoading = false;
        this.mBikeParts = null;
        this.mDrawZoneWidth = 0;
        this.mDrawZoneHeight = 0;
        this.mOffsetX = 0;
        this.mOffsetY = 0;
        this.mPaddingTop = 0;
        this.mPart4OffsetX = 0.0f;
        this.mPart4OffsetY = 0.0f;
        this.mPart5OffsetX = 0.0f;
        this.mPart5OffsetY = 0.0f;
        this.mAnimOffsetBegin = 0.6f;
        this.mAnimOffsetEnd = 1.0f;
        this.mRotateDegree = 0.0f;
        init(context);
    }

    private void init(Context context) {
        PtrLocalDisplay.init(getContext());
        int dp2px = PtrLocalDisplay.dp2px(6.0f);
        this.mPaddingTop = dp2px;
        setPadding(0, dp2px, 0, 0);
        this.mDrawZoneWidth = PtrLocalDisplay.dp2px(44.0f);
        this.mDrawZoneHeight = PtrLocalDisplay.dp2px(32.0f);
        this.mPart4OffsetX = 0.0f;
        this.mPart4OffsetY = PtrLocalDisplay.dp2px(10.0f);
        this.mPart5OffsetX = PtrLocalDisplay.dp2px(30.0f);
        this.mPart5OffsetY = PtrLocalDisplay.dp2px(10.0f);
        this.mRotateDegree = 0.0f;
        Bitmap[] bitmapArr = new Bitmap[5];
        this.mBikeParts = bitmapArr;
        bitmapArr[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_refresh_bike_1);
        this.mBikeParts[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_refresh_bike_2);
        this.mBikeParts[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_refresh_bike_3);
        this.mBikeParts[3] = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_refresh_bike_4);
        this.mBikeParts[4] = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_refresh_bike_4);
    }

    private void setProgress(float f) {
        this.mProgress = f;
    }

    private int getTopOffset() {
        return getPaddingTop() + PtrLocalDisplay.dp2px(6.0f);
    }

    private int getBottomOffset() {
        return getPaddingBottom() + PtrLocalDisplay.dp2px(6.0f);
    }

    @Override
    protected void onMeasure(int i, int i2) {
        super.onMeasure(i, View.MeasureSpec.makeMeasureSpec(getTopOffset() + this.mDrawZoneHeight + getBottomOffset(), MeasureSpec.EXACTLY));//1073741824
        this.mOffsetX = (getMeasuredWidth() - this.mDrawZoneWidth) / 2;
        this.mOffsetY = getTopOffset();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        if (this.mIsInLoading) {
            this.mRotateDegree = (this.mRotateDegree - 10.0f) % 360.0f;
            drawBike(canvas, 7, 1.0f);
        } else {
            float f = this.mProgress;
            if (f >= 1.0f) {
                drawBike(canvas, 6, 1.0f);
            } else {
                float realProgress = getRealProgress(f, this.mAnimOffsetBegin, this.mAnimOffsetEnd);
                if (realProgress <= 0.0f || realProgress > 0.2d) {
                    double d = realProgress;
                    if (d > 0.2d && d <= 0.4d) {
                        drawBike(canvas, 2, realProgress);
                    } else if (d > 0.4d && d <= 0.6d) {
                        drawBike(canvas, 3, realProgress);
                    } else if (d > 0.6d && d <= 0.8d) {
                        drawBike(canvas, 4, realProgress);
                    } else if (d > 0.8d && d <= 1.0d) {
                        drawBike(canvas, 5, realProgress);
                    }
                } else {
                    drawBike(canvas, 1, realProgress);
                }
            }
        }
        canvas.restore();
        if (this.mIsInLoading) {
            invalidate();
        }
    }

    private void drawBike(Canvas canvas, int i, float f) {
        Matrix matrix = new Matrix();
        switch (i) {
            case 1:
                matrix.postTranslate(this.mOffsetX, this.mOffsetY);
                this.mPaint.setAlpha((int) ((f / 0.2d) * 255.0d));
                canvas.drawBitmap(this.mBikeParts[0], matrix, this.mPaint);
                return;
            case 2:
                matrix.postTranslate(this.mOffsetX, this.mOffsetY);
                this.mPaint.setAlpha(255);
                canvas.drawBitmap(this.mBikeParts[0], matrix, this.mPaint);
                this.mPaint.setAlpha((int) (((f - 0.2d) / 0.2d) * 255.0d));
                canvas.drawBitmap(this.mBikeParts[1], matrix, this.mPaint);
                return;
            case 3:
                matrix.postTranslate(this.mOffsetX, this.mOffsetY);
                this.mPaint.setAlpha(255);
                canvas.drawBitmap(this.mBikeParts[0], matrix, this.mPaint);
                canvas.drawBitmap(this.mBikeParts[1], matrix, this.mPaint);
                this.mPaint.setAlpha((int) (((f - 0.4d) / 0.2d) * 255.0d));
                canvas.drawBitmap(this.mBikeParts[2], matrix, this.mPaint);
                return;
            case 4:
                matrix.postTranslate(this.mOffsetX, this.mOffsetY);
                this.mPaint.setAlpha(255);
                canvas.drawBitmap(this.mBikeParts[0], matrix, this.mPaint);
                canvas.drawBitmap(this.mBikeParts[1], matrix, this.mPaint);
                canvas.drawBitmap(this.mBikeParts[2], matrix, this.mPaint);
                matrix.reset();
                matrix.postTranslate(this.mOffsetX, this.mOffsetY + this.mPart4OffsetY);
                this.mPaint.setAlpha((int) (((f - 0.6d) / 0.2d) * 255.0d));
                canvas.drawBitmap(this.mBikeParts[3], matrix, this.mPaint);
                return;
            case 5:
                matrix.postTranslate(this.mOffsetX, this.mOffsetY);
                this.mPaint.setAlpha(255);
                canvas.drawBitmap(this.mBikeParts[0], matrix, this.mPaint);
                canvas.drawBitmap(this.mBikeParts[1], matrix, this.mPaint);
                canvas.drawBitmap(this.mBikeParts[2], matrix, this.mPaint);
                matrix.reset();
                matrix.postTranslate(this.mOffsetX, this.mOffsetY + this.mPart4OffsetY);
                canvas.drawBitmap(this.mBikeParts[3], matrix, this.mPaint);
                matrix.reset();
                matrix.postTranslate(this.mOffsetX + this.mPart5OffsetX, this.mOffsetY + this.mPart5OffsetY);
                this.mPaint.setAlpha((int) (((f - 0.8d) / 0.2d) * 255.0d));
                canvas.drawBitmap(this.mBikeParts[4], matrix, this.mPaint);
                return;
            case 6:
                matrix.postTranslate(this.mOffsetX, this.mOffsetY);
                this.mPaint.setAlpha(255);
                canvas.drawBitmap(this.mBikeParts[0], matrix, this.mPaint);
                canvas.drawBitmap(this.mBikeParts[1], matrix, this.mPaint);
                canvas.drawBitmap(this.mBikeParts[2], matrix, this.mPaint);
                matrix.reset();
                matrix.postTranslate(this.mOffsetX, this.mOffsetY + this.mPart4OffsetY);
                canvas.drawBitmap(this.mBikeParts[3], matrix, this.mPaint);
                matrix.reset();
                matrix.postTranslate(this.mOffsetX + this.mPart5OffsetX, this.mOffsetY + this.mPart5OffsetY);
                canvas.drawBitmap(this.mBikeParts[4], matrix, this.mPaint);
                return;
            case 7:
                matrix.postTranslate(this.mOffsetX, this.mOffsetY);
                this.mPaint.setAlpha(255);
                canvas.drawBitmap(this.mBikeParts[0], matrix, this.mPaint);
                canvas.drawBitmap(this.mBikeParts[1], matrix, this.mPaint);
                canvas.drawBitmap(this.mBikeParts[2], matrix, this.mPaint);
                float width = this.mBikeParts[3].getWidth();
                float height = this.mBikeParts[3].getHeight();
                matrix.reset();
                matrix.postTranslate((-width) / 2.0f, (-height) / 2.0f);
                matrix.postRotate(this.mRotateDegree);
                matrix.postTranslate(width / 2.0f, height / 2.0f);
                matrix.postTranslate(this.mOffsetX, this.mOffsetY + this.mPart4OffsetY);
                canvas.drawBitmap(this.mBikeParts[3], matrix, this.mPaint);
                matrix.postTranslate(this.mPart5OffsetX, 0.0f);
                canvas.drawBitmap(this.mBikeParts[4], matrix, this.mPaint);
                return;
            default:
                return;
        }
    }

    private void startLoading() {
        this.mIsInLoading = true;
    }

    private void stopLoading() {
        this.mIsInLoading = false;
    }

    @Override
    public void onUIRefreshBegin(PtrFrameLayout ptrFrameLayout) {
        startLoading();
    }

    @Override
    public void onUIRefreshComplete(PtrFrameLayout ptrFrameLayout) {
        stopLoading();
    }

    @Override
    public void onUIPositionChange(PtrFrameLayout ptrFrameLayout, boolean z, byte b, PtrIndicator ptrIndicator) {
        setProgress(Math.min(1.0f, ptrIndicator.getCurrentPercent()));
        invalidate();
    }
}
